"""Golang相关的衍生配置处理"""

from typing import Dict, Any
from src.base import bera

def derive_golang_config(meta_info: Dict[str, Any]) -> Dict[str, Any]:
    """处理meta.json的Golang相关衍生配置

    处理字段级衍生配置:
    - columns.*.derived.golang.type: Go类型
    - columns.*.derived.golang.import: 需要的import包
    - columns.*.derived.golang.default_value: 默认值
    - columns.*.derived.golang.json_tag: JSON标签
    - columns.*.derived.golang.gorm_tag: GORM标签

    :param meta_info: meta.json格式的结构体
    :return: 更新后的结构体,可以继续传递给其他derived函数处理
    """
    bera.logger.debug("====== 开始处理Golang相关衍生配置 ======")

    # 遍历处理每个column
    for column_name, column_info in meta_info["columns"].items():
        try:
            # 基于Java类型转换为Go类型
            java_type = column_info.get("derived", {}).get("java", {}).get("type", "String")

            # Java类型到Go类型的映射
            java_to_go_type_map = {
                "String": "string",
                "Integer": "int",
                "Long": "int64",
                "BigDecimal": "float64",
                "LocalDate": "time.Time",
                "LocalDateTime": "time.Time",
                "Date": "time.Time",
                "Boolean": "bool"
            }

            go_type = java_to_go_type_map.get(java_type, "string")

            # 需要import的包
            go_import = ""
            if go_type == "time.Time":
                go_import = "time"

            # 默认值
            go_default_value = {
                "string": '""',
                "int": "0",
                "int64": "0",
                "float64": "0.0",
                "time.Time": "time.Time{}",
                "bool": "false"
            }.get(go_type, '""')

            # JSON标签
            json_tag = column_info.get("naming", {}).get("field_name", column_name)

            # GORM标签构建
            gorm_tag_parts = [f"column:{column_info['sql']['name']}"]
            if column_info["sql"].get("primary_key", False):
                gorm_tag_parts.append("primaryKey")
            if column_info["sql"].get("auto_increment", False):
                gorm_tag_parts.append("autoIncrement")
            if not column_info["sql"].get("nullable", True):
                gorm_tag_parts.append("not null")
            gorm_tag = ";".join(gorm_tag_parts)

            # 添加derived配置
            if "derived" not in column_info:
                column_info["derived"] = {}
            if "golang" not in column_info["derived"]:
                column_info["derived"]["golang"] = {}

            column_info["derived"]["golang"]["type"] = go_type
            column_info["derived"]["golang"]["import"] = go_import
            column_info["derived"]["golang"]["default_value"] = go_default_value
            column_info["derived"]["golang"]["json_tag"] = json_tag
            column_info["derived"]["golang"]["gorm_tag"] = gorm_tag

        except Exception as e:
            bera.logger.warning(f"处理列 {column_name} 的Golang配置时出错: {str(e)}")
            # 如果转换失败,使用默认配置
            if "derived" not in column_info:
                column_info["derived"] = {}
            if "golang" not in column_info["derived"]:
                column_info["derived"]["golang"] = {}

            column_info["derived"]["golang"]["type"] = "string"
            column_info["derived"]["golang"]["import"] = ""
            column_info["derived"]["golang"]["default_value"] = '""'
            column_info["derived"]["golang"]["json_tag"] = column_name
            column_info["derived"]["golang"]["gorm_tag"] = f"column:{column_info['sql']['name']}"

    bera.logger.debug("====== Golang相关衍生配置处理完成 ======")
    return meta_info